<template>
  <el-row type="flex" :style="isRoot? 'height:40px' : 'width:100%'" justify="space-between">
    <el-col>
      <span>{{ treeData.name }}</span>
    </el-col>
    <el-col :span="6">
      <el-row type="flex" justify="space-between">
        <!-- 右1 -->
        <el-col>
          <span>{{ treeData.manager }}</span>
        </el-col>
        <!-- 右二 -->
        <el-col>
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addHandler">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editHander">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delHandler">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentData } from '@/api/department'
export default {
  // 外部传入的这行数据
  name: 'TreeTools',
  props: {
    treeData: {
      type: Object,
      default: () => ({})
    },
    // 是否为根
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    async  delHandler() {
      // 确认框
      await this.$confirm('确认删除吗', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
      // 发送删除请求
      await delDepartmentData(this.treeData.id)
      // 提示成功
      this.$message.success('删除成功')
      // 更新列表
      this.$emit('update')
    },
    addHandler() {
      this.$emit('addDept', this.treeData)
    },
    editHander() {
      this.$emit('editDept', this.treeData)
    }
  }

}
</script>

<style>

</style>
